package com.client.widgets;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginWidget extends Composite {
	
	private RootWidget rootWidget;
	
	//this SimplePanel will contain the entire widget
	private SimplePanel loginWidgetPanel;
	//this vertical panel will be displayed in the loginWidgetPanel when the user is logged out
	
	private VerticalPanel loginVPanel;	
	private TextBox usernameBox;
	private PasswordTextBox passwordBox;
	private Button loginButton;
	private Button registerButton;

	
	// this button will be displayed when the user is logged in instead of loginVPanel
	private Button logoutButton;
	
	
	
	public LoginWidget(RootWidget root) {
		//the usual inits
		this.loginWidgetPanel = new SimplePanel();
		
		this.loginVPanel = new VerticalPanel();
		this.usernameBox = new TextBox();
		this.passwordBox = new PasswordTextBox();
		this.rootWidget = root;
		this.loginButton = new Button("login");
		this.logoutButton = new Button("logout");
		this.registerButton = new Button("register");
		
		this.usernameBox.setText("username");
		this.passwordBox.setText("password");
		
		this.loginWidgetPanel.setWidget(loginVPanel);
		this.loginVPanel.add(this.usernameBox);
		this.loginVPanel.add(this.passwordBox);
		
		HorizontalPanel buttonHPanel = new HorizontalPanel();
		this.loginVPanel.add(buttonHPanel);
		buttonHPanel.add(this.loginButton);
		buttonHPanel.add(this.registerButton);
		
		addListeners();
		
		//initWidget is called on the outermost containing panel
		initWidget(this.loginWidgetPanel);
	}
	
	private void addListeners() {
		//make it so usernameBox says "username" unless clicked on, in which case it clears
		//and displays "username" again if focus is lost and nothing is typed
		this.usernameBox.addFocusListener(new FocusListener() {
			public void onFocus(Widget sender) {
				if(usernameBox.getText().equals("username"))
					usernameBox.setText("");
			}
			public void onLostFocus(Widget sender) {
				if(usernameBox.getText().equals(""))
					usernameBox.setText("username");
			}
		});
		
		//do the same for passwordBox
		this.passwordBox.addFocusListener(new FocusListener() {
			public void onFocus(Widget sender) {
				if(passwordBox.getText().equals("password"))
					passwordBox.setText("");
			}
			public void onLostFocus(Widget sender) {
				if(passwordBox.getText().equals(""))
					passwordBox.setText("password");
			}
		});
		
		this.loginButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				login();
			}
		});
		
		this.logoutButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				logout();
			}
		});
		
		this.registerButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				register();
			}
		});
	}

	//this method is called when the "login" button is pressed
	private void login() {
		//first the username-password combination must be verified with the database
		if(validateUserPassCombo()) {
			//if the combination is valid, then the login widget must transform into 
			//a single logout button
			this.loginWidgetPanel.setWidget(this.logoutButton);
		}
	}
	
	
	private boolean validateUserPassCombo() {
		String name = this.usernameBox.getText();
		String password = this.passwordBox.getText();
		
		
		return true;
	}

	private void logout() {
		this.loginWidgetPanel.setWidget(this.loginVPanel);
	}
	
	private void register() {

	}
	
	
}
